package com.eastmoney.emis.edda.domain.entity;

import com.eastmoney.emis.edda.domain.valueobject.EddiStatus;
import com.eastmoney.emis.edda.domain.valueobject.MoneyType;
import com.eastmoney.emis.edda.domain.valueobject.TimeFormatter;
import com.eastmoney.emis.edda.infrastructure.eddi.po.EmhkBankTrademidPo;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * @description: Description
 * @projectName: EDDA-HSBC
 * @package: com.eastmoney.emis.edda.domain.entity
 * @author: 汪程程
 * @date: 2022/10/11 1:26 PM
 * @version: 1.0
 */
public class EddiInstruction {

    private String eid;
    private String clientId;
    private String bankCode;
    private String bankAccount;
    private String applySerialNo;
    private String mandateId;
    private String status;
    private String moneyType;
    private BigDecimal occurBalance;
    private String arrivalTime;
    // hsbc 返回的流水号
    private String transactionId;
    // 资金存返回的流水号
    private String serialNo;

    private String note;

    private LocalDateTime processTime;
    private String processUserName;

    private String processUid;

    private LocalDateTime createdTime;
    private LocalDateTime updatedTime;

    public EddiInstruction() {
        this.status = EddiStatus.INIT.value();
        this.moneyType = MoneyType.HKD.getId().toString();
        this.createdTime = LocalDateTime.now();
        this.updatedTime = LocalDateTime.now();
    }
    public EddiInstruction(EddaBind eddaBind) {
        this();
        this.bankCode = eddaBind.getBankCode();
        this.bankAccount = eddaBind.getBankAccount();
        this.clientId = eddaBind.getClientId();
        this.mandateId = eddaBind.getMandateId();
    }

    public EddiInstruction(EmhkBankTrademidPo emhkBankTrademidPo) {
        this.eid = emhkBankTrademidPo.EID;
        this.clientId = emhkBankTrademidPo.HKCUACCID;
        this.applySerialNo = emhkBankTrademidPo.APPLYSERIALNO;
        this.mandateId = emhkBankTrademidPo.MANDATEID;
        this.status = emhkBankTrademidPo.STATUS;
        this.moneyType = emhkBankTrademidPo.MONEYTYPE;
        this.occurBalance = emhkBankTrademidPo.OCCURBALANCE;
        this.arrivalTime = emhkBankTrademidPo.ARRIVALTIME;
        this.transactionId = emhkBankTrademidPo.TRANSACTIONID;
        this.serialNo = emhkBankTrademidPo.SERIALNO;
        this.bankCode = emhkBankTrademidPo.BANKCODE;
        this.bankAccount = emhkBankTrademidPo.BANKACCOUNT;
        this.processTime = emhkBankTrademidPo.PROCESSUSERTIME;
        this.createdTime = emhkBankTrademidPo.EITIME;
        this.updatedTime = emhkBankTrademidPo.EUTIME;
        this.note = emhkBankTrademidPo.NOTE;

    }

    public String getEid() {
        return eid;
    }

    public void setEid(String eid) {
        this.eid = eid;
    }

    public String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public String getApplySerialNo() {
        return applySerialNo;
    }

    public void setApplySerialNo(String applySerialNo) {
        this.applySerialNo = applySerialNo;
    }

    public String getMandateId() {
        return mandateId;
    }

    public void setMandateId(String mandateId) {
        this.mandateId = mandateId;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public void setStatus(EddiStatus status) {
        if(status != null) {
            this.status = status.value();
        }
    }

    public String getMoneyType() {
        return moneyType;
    }

    public void setMoneyType(String moneyType) {
        this.moneyType = moneyType;
    }

    public BigDecimal getOccurBalance() {
        return occurBalance;
    }

    public void setOccurBalance(BigDecimal occurBalance) {
        this.occurBalance = occurBalance;
    }

    public String getArrivalTime() {
        return arrivalTime;
    }

    public void setArrivalTime(String arrivalTime) {
        this.arrivalTime = arrivalTime;
    }

    public String getTransactionId() {
        return transactionId;
    }

    public void setTransactionId(String transactionId) {
        this.transactionId = transactionId;
    }

    public String getSerialNo() {
        return serialNo;
    }

    public void setSerialNo(String serialNo) {
        this.serialNo = serialNo;
    }

    public String getNote() {
        return note;
    }

    public void setNote(String note) {
        this.note = note;
    }

    public String getBankCode() {
        return bankCode;
    }

    public void setBankCode(String bankCode) {
        this.bankCode = bankCode;
    }

    public String getBankAccount() {
        return bankAccount;
    }

    public void setBankAccount(String bankAccount) {
        this.bankAccount = bankAccount;
    }

    public LocalDateTime getProcessTime() {
        return processTime;
    }

    public void setProcessTime(LocalDateTime processTime) {
        this.processTime = processTime;
    }

    public String getProcessUserName() {
        return processUserName;
    }

    public void setProcessUserName(String processUserName) {
        this.processUserName = processUserName;
    }

    public String getProcessUid() {
        return processUid;
    }

    public void setProcessUid(String processUid) {
        this.processUid = processUid;
    }

    public LocalDateTime getCreatedTime() {
        return createdTime;
    }

    public void setCreatedTime(LocalDateTime createdTime) {
        this.createdTime = createdTime;
    }

    public LocalDateTime getUpdatedTime() {
        return updatedTime;
    }

    public void setUpdatedTime(LocalDateTime updatedTime) {
        this.updatedTime = updatedTime;
    }

    public void close() {
        this.status = EddiStatus.INVALID.value();
        this.processTime = LocalDateTime.now();
        this.processUserName = "system";
    }
    public void serialNo(String serialNo, LocalDateTime dateTime) {
        this.setSerialNo(TimeFormatter.YYYYMMDD.format(dateTime) + serialNo);
    }

    private static final String SEPARATOR = " # ";
    public String getRemark() {
        return "EDDA FUND DEPOSIT - HSBC" + SEPARATOR + this.transactionId;
    }


    @Override
    public String toString() {
        return "EddiInstruction{" +
                "eid='" + eid + '\'' +
                ", clientId='" + clientId + '\'' +
                ", bankCode='" + bankCode + '\'' +
                ", bankAccount='" + bankAccount + '\'' +
                ", applySerialNo='" + applySerialNo + '\'' +
                ", mandateId='" + mandateId + '\'' +
                ", status='" + status + '\'' +
                ", moneyType='" + moneyType + '\'' +
                ", occurBalance=" + occurBalance +
                ", arrivalTime='" + arrivalTime + '\'' +
                ", transactionId='" + transactionId + '\'' +
                ", serialNo='" + serialNo + '\'' +
                ", note='" + note + '\'' +
                '}';
    }

    public void finish(String serialNo) {
        LocalDateTime now = LocalDateTime.now();
        this.setArrivalTime(TimeFormatter.YYYY_MM_DD_HH_mm_ss.format(now));
        this.serialNo(serialNo, now);
        this.setStatus(EddiStatus.FUND_DEPOSIT_SUCCESS);
    }
}
